<?php
define('web_s', 'johnsongod');
require dirname(__FILE__) . '/includes/init.php';
require adminroot.'includes/session.php';
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'list';
$smarty->assign('act', $act);
checkpermissions('goods');
if($act == 'list'){
	$data = paging('m_goods', 'goods.php?act=list');
	$title = '摩托列表';
	$smarty->assign('data', $data);
}elseif($act == 'add' || $act == 'edit'){
	$title = '添加摩托';
	if($act == 'edit'){
		$title = '编辑摩托';
		$goods_id = isset($_REQUEST['goods_id']) ? $_REQUEST['goods_id'] : '';
		if(!$goods_id){
			exit('id为空');
		}
		$goods = $db->getRow("select * from m_goods where `goods_id` = '{$goods_id}'");
		if(!$goods){
			exit('id错误');
		}
		$goods['expiration_time'] = (floor(($goods['expiration_time'] - mktime(0,0,0,date('m'),date('d'),date('Y')))/24/60/60)) <= 0 ? 0 : floor(($goods['expiration_time'] - mktime(0,0,0,date('m'),date('d'),date('Y')))/24/60/60);
		$goods['piclist'] = $db->getAll("select * from m_goods_pic where goods_id = '{$goods_id}'");
		$smarty->assign('goods', $goods);
	}
	$tempimglist = $db->getAll("select * from m_goods_pic where goods_id = '0' order by id desc");
	$smarty->assign('tempimglist', $tempimglist);
}elseif($act == 'addsubmit' || $act == 'editsubmit'){
	if($act == 'editsubmit'){
		$goods_id = isset($_REQUEST['goods_id']) ? $_REQUEST['goods_id'] : '';
		if(!$goods_id){
			exit('goods_id为空');
		}
		$goods = $db->getRow("select * from m_goods where goods_id = '{$goods_id}'");
		if(!$goods){
			exit('goods_id错误');
		}
	}
	$data['expiration_time'] = isset($_POST['expiration_time']) ? $_POST['expiration_time']*24*60*60 + mktime(0,0,0,date('m'),date('d'),date('Y')) : 0;
	$data['goods_name'] = isset($_POST['goods_name']) ? $_POST['goods_name'] : '';
	$data['area_id'] = isset($_POST['area_id']) ? $_POST['area_id'] : '';
	$data['type_id'] = isset($_POST['type_id']) ? $_POST['type_id'] : '';
	$data['goods_price'] = isset($_POST['goods_price']) ? $_POST['goods_price'] : '';
	$data['models_id'] = isset($_POST['models_id']) ? $_POST['models_id'] : '';
	$data['condition_id'] = isset($_POST['condition_id']) ? $_POST['condition_id'] : '';
	$data['displacement_id'] = isset($_POST['displacement_id']) ? $_POST['displacement_id'] : '';
	$data['color_id'] = isset($_POST['color_id']) ? $_POST['color_id'] : '';
	$data['buy_year'] = isset($_POST['buy_year']) ? $_POST['buy_year'] : '';
	$data['mileage_id'] = isset($_POST['mileage_id']) ? $_POST['mileage_id'] : '';
	$data['goods_info'] = isset($_POST['goods_info']) ? $_POST['goods_info'] : '';
	$data['phone'] = isset($_POST['phone']) ? $_POST['phone'] : '';
	$data['contact'] = isset($_POST['contact']) ? $_POST['contact'] : '';
	foreach($data as $k => $v){
		if(!$v){
			exit($lan[$k].'为空');
		}
	}
	$data['qq_msn'] = isset($_POST['qq_msn']) ? $_POST['qq_msn'] : '';
	$data['is_enable'] = isset($_POST['is_enable']) ? $_POST['is_enable'] : '1';
	$data['add_time'] = time();
	$r = $act == 'addsubmit' ? $db->autoExecute('m_goods', $data, 'INSERT') : $db->autoExecute('m_goods', $data, 'UPDATE', " goods_id = '{$goods_id}'");
	$piclist = isset($_POST['piclist']) ? $_POST['piclist'] : array();
	$goods_id = isset($goods_id) ? $goods_id : $db->insert_id();
	foreach($piclist as $k => $v){
		$db->autoExecute('m_goods_pic', array('goods_id' => $goods_id), 'UPDATE', " id = '{$v}'");
	}
	header('Location: goods.php?act=list');
}elseif($act == 'del'){
	$goods_id = isset($_REQUEST['goods_id']) ? $_REQUEST['goods_id'] : '';
	if(!$goods_id){
		exit('id为空');
	}
	$r = $db->getOne("select count(*) from m_goods where goods_id = '{$goods_id}'");
	if(!$r){
		exit('id错误');
	}
	$r = $db->query("delete from m_goods where goods_id = '{$goods_id}'");
	if($r){
		$piclist = $db->getAll("select * from m_goods_pic where goods_id = '{$goods_id}'");
		foreach($piclist as $key => $value){
			@unlink('/images/'.$value['thumb']);
			@unlink('/images/'.$value['pic_url']);
		}
		$db->query("delete from m_goods_pic where goods_id = '{$goods_id}'");
		header('Location: goods.php?act=list');
	}
}elseif($act == 'delpic'){
	$goods_id = isset($_GET['goods_id']) ? $_GET['goods_id'] : 0;
	$id = isset($_GET['id']) ? $_GET['id'] : '';
	if(!$id){
		exit;
	}
	$r = $db->getRow("select * from m_goods_pic where id = '{$id}'");
	if(!$r){
		exit;
	}
	@unlink('/images/'.$r['pic_url']);
	@unlink('/images/'.$r['thumb']);
	$r = $db->query("delete from m_goods_pic where id = '{$id}'");
	header("Location: piclist.php?goods_id={$goods_id}");
}elseif($act == 'recommend'){
	$goods_id = isset($_REQUEST['goods_id']) ? $_REQUEST['goods_id'] : '';
	if(!$goods_id){
		exit('id为空');
	}
	$r = $db->getRow("select * from m_goods where goods_id = '{$goods_id}'");
	if(!$r){
		exit('id错误');
	}
	$r = $db->query("update m_goods set is_recommend = '".($r['is_recommend'] == 1 ? 0 : 1)."' where goods_id = '{$goods_id}'");
	header('Location: goods.php?act=list');
}elseif($act == 'new'){
	$goods_id = isset($_REQUEST['goods_id']) ? $_REQUEST['goods_id'] : '';
	if(!$goods_id){
		exit('id为空');
	}
	$r = $db->getRow("select * from m_goods where goods_id = '{$goods_id}'");
	if(!$r){
		exit('id错误');
	}
	$r = $db->query("update m_goods set is_new = '".($r['is_new'] == 1 ? 0 : 1)."' where goods_id = '{$goods_id}'");
	header('Location: goods.php?act=list');
}
$smarty->assign('title', $title);

$smarty->display('goods.htm');
